<?php
namespace Admin\Controller;

class ProductController extends BaseController{
    public function index(){
    	$Product = D('Product');
    	$object = $Product->all($this->page($Product->total()));
    	if ($object){
    		$Psort = D('Psort');
	    	foreach ($object as $key=>$value){
	    		$object2 = $Psort->one($value['sid']);
	    		$object[$key]['psort'] = $object2 ? '<span style="color:'.$object2['color'].';">'.$object2['name'].'</span>' : '此分类已被删除';
	    	}
    	}
    	$this->assign('All',$object);
		$this->psort(I('get.sid'));
    	$this->display();
    }
    
    public function add(){
    	if (IS_POST){
    		$Product = D('Product');
    		$object = $Product->insert();
    		if (is_numeric($object)){
    			$object>0 ? $this->success(U(CONTROLLER_NAME.'/index'),'产品添加成功！') : $this->failed('产品添加失败！');
    		}else{
    			$this->failed($object);
    		}
    	}
    	$this->psort();
    	$this->display();
    }
    
    public function update(){
		if (I('get.id')){
			$Product = D('Product');
			if (IS_POST){
				$object = $Product->update();
				is_numeric($object) ? $this->success(U(CONTROLLER_NAME.'/index'),'产品修改成功！') : $this->failed($object);
			}
			$object = $Product->one();
			if (!$object) $this->failed('不存在此产品！');
    		$this->psort($object['sid']);
			$this->assign('One',$object);
			$this->display();
		}else{
			$this->failed('非法操作！');
		}
    }
    
    public function selected(){
		if (I('get.id')){
			$Product = D('Product');
    		if (!$Product->one()) $this->failed('不存在此产品！');
    		if (!$Product->selected()) $this->failed('设置默认产品失败！');
    		$this->success(C('PREV_URL'));
    	}else{
    		$this->failed('非法操作！');
    	}
    }
	
    public function sort(){
    	if (IS_POST){
    		$Product = D('Product');
    		foreach (I('post.sort') as $key=>$value){
    			if (!is_numeric($value)) continue;
    			$Product->sort($key,$value);
    		}
    		$this->success(C('PREV_URL'),'产品排序成功！');
    	}
    }
     
    public function view(){
		if (I('get.id')){
			$Product = D('Product');
			$object = $Product->one();
    		if (!$object) $this->failed('不存在此产品！');
    		if ($object['view'] == 0){
    			if (!$Product->view(1)) $this->failed('设置产品前台显示失败！');
    		}else{
    			if (!$Product->view(0)) $this->failed('取消产品前台显示失败！');
    		}
    		$this->success(C('PREV_URL'));
    	}else{
    		$this->failed('非法操作！');
    	}
    }
    
    public function delete(){
		if (I('get.id')){
			//$this->failed('演示站，数据无法删除！');
			$Product = D('Product');
			if (IS_POST) $Product->remove() ? $this->success(I('post.prev'),'产品删除成功！') : $this->failed('产品删除失败！');
			if (!$Product->one()) $this->failed('不存在此产品！');
    		$this->confirm('您真的要删除这条数据么？');
    	}else{
    		$this->failed('非法操作！');
    	}
    }
    
    private function psort($n=0){
    	$html = '';
    	$Psort = D('Psort');
    	$object = $Psort->all2();
    	if ($object){
	    	foreach ($object as $value){
	    		$html .= '<option value="'.$value['id'].'"'.($value['id']==$n ? ' selected' : '').' style="color:'.$value['color'].';">'.$value['name'].'</option>';
	    	}
    	}
    	$this->assign('Sort',$html);
    }
}